package com.retail.common.result;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;

import java.io.Serializable;

/**
 * 统一响应结果
 *
 * @author retail
 * @since 2024-01-01
 */
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Result<T> implements Serializable {

  private static final long serialVersionUID = 1L;

  /**
   * 状态码
   */
  private Integer code;

  /**
   * 响应消息
   */
  private String message;

  /**
   * 响应数据
   */
  private T data;

  /**
   * 时间戳
   */
  private Long timestamp;

  public Result() {
    this.timestamp = System.currentTimeMillis();
  }

  public Result(Integer code, String message) {
    this();
    this.code = code;
    this.message = message;
  }

  public Result(Integer code, String message, T data) {
    this(code, message);
    this.data = data;
  }

  /**
   * 成功响应
   */
  public static <T> Result<T> success() {
    return new Result<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage());
  }

  /**
   * 成功响应
   */
  public static <T> Result<T> success(T data) {
    return new Result<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), data);
  }

  /**
   * 成功响应
   */
  public static <T> Result<T> success(String message, T data) {
    return new Result<>(ResultCode.SUCCESS.getCode(), message, data);
  }

  /**
   * 失败响应
   */
  public static <T> Result<T> error() {
    return new Result<>(ResultCode.ERROR.getCode(), ResultCode.ERROR.getMessage());
  }

  /**
   * 失败响应
   */
  public static <T> Result<T> error(String message) {
    return new Result<>(ResultCode.ERROR.getCode(), message);
  }

  /**
   * 失败响应
   */
  public static <T> Result<T> error(Integer code, String message) {
    return new Result<>(code, message);
  }

  /**
   * 失败响应
   */
  public static <T> Result<T> error(ResultCode resultCode) {
    return new Result<>(resultCode.getCode(), resultCode.getMessage());
  }

  /**
   * 判断是否成功
   */
  public boolean isSuccess() {
    return ResultCode.SUCCESS.getCode().equals(this.code);
  }
}
